<template>
	<view class="staffCustomer">
		<nav-bars v-if="moduleType=='superHK'" bgColor="transparent" fontColor="#fff" :title="type=='mine'?'我的客户':'团队客户'"></nav-bars>
		<nav-bars v-else bgColor="transparent" fontColor="#fff" :title="type=='mine'?'我的客户':'企业全部客户'"></nav-bars>
		<view class="topBg">
			<view class="search">
				<view class="searchBox">
					<text class="iconfont">&#xe68e;</text>
					<input type="text" @input="clearInput" confirm-type="search" @confirm="searchFn" value="" v-model="searchInput" placeholder="搜索客户" />
				</view>
				<!-- <image :src="`${baseImg}/static/adminShop/client/select.png`" mode=""></image> -->
			</view>
		</view>
		<view class="content" :style="'margin-top:'+navHeight+'rpx;'">
			<view class="selectBox" v-if="type!='mine'">
				<view class="item" @click="toScreenStaff">
					<view v-if="!staffList.length">全部添加人</view>
					<view v-else>{{staffList[0].name?staffList[0].name:staffList[0].nickname}}等{{staffList.length}}个员工</view>
					<text v-if="!staffList.length" class="iconfont">&#xe693;</text>
					<text v-else class="iconfont closeIcon" @click.stop="closeSelect(1)">&#xe6af;</text>
				</view>
				<view class="item" @click="toScreenTag">
					<view v-if="!tagsArr.length">不限标签</view>
					<view v-else>{{tagsArr[0].tag_name}}等{{tagsArr.length}}个标签</view>
					<text v-if="!tagsArr.length" class="iconfont">&#xe693;</text>
					<text v-else class="iconfont closeIcon" @click.stop="closeSelect(2)">&#xe6af;</text>
				</view>
			</view>
			<view class="indexList">
				<!-- <u-index-list :scrollTop="scrollTop" z-index="1" :offset-top="navHeight+156" @select="selectFn">
					<view v-for="(item, index) in customerList" :key="index">
						<u-index-anchor :index="item.sort"/>
						<view class="list-cell" @click="selectExpress(item1)" v-for="(item1,index1) in item.list" :key="index1">
							<image :src="item1.logo" mode=""></image>
							{{item1.express_name}}
						</view>
					</view>
				</u-index-list> -->
				<view class="item" v-for="(item,index) in customerList" :key="index" @click="toDetail(item.staff_qy_user_id,item.external_userid)">
					<image :src="item.external_data.avatar" mode=""></image>
					<view class="info" :class="{'active':!item.follow_data.tags.length}">
						<view class="name">
							<view class="omit-1" style="max-width: 280rpx;">
								{{item.external_data.name}}
							</view>
							<view class="tagBox" v-if="item.follow_data.tags.length">
								<text v-for="(item1,index1) in item.follow_data.tags" :key="index1">{{item1.tag_name}}</text>
								<!-- <text>重要</text> -->
							</view>
						</view>
						<view class="source">
							{{item.follow_data.add_way}}
							<text>{{$utils.getFormatDate(item.create_time*1000,'yyyy-MM-dd')}}</text>
						</view>
					</view>
				</view>
				<u-loadmore :status="status" :icon-type="iconType" :load-text="loadText" />
			</view>
		</view>
	</view>
</template>
<script>
	import service_client from '@/service/client.js';
	import { GetTeamCustomer } from '@/service/superHK.js';
export default {
	name: '',
	components: {},
	data() {
		return {
			status: 'loadmore',
			iconType: 'flower',
			loadText: {
				loadmore: '轻轻上拉',
				loading: '努力加载中',
				nomore: '实在没有了~'
			},
			qyUserId:'',//跳转传进来的qy_user_id
			type:'',//mine-我的客户
			tagsArr:[], //筛选选中的标签
			staffList:[], //筛选选中的添加人
			baseImg:this.$static,
			searchInput:'',
			navHeight:100,
			scrollTop:0,
			indexList: ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
				"V", "W", "X", "Y", "Z"
			],
			customerList:[],
			page:1,
			pagesize:10,
			moduleType:'',
		};
	},
	onLoad(options) {
		let barHeight = uni.getStorageSync('StatusBarHeight').height*2
		this.navHeight = 308 - barHeight
		this.qyUserId = options.qyUserId
		this.type = options.type
		this.moduleType = options.moduleType
	},
	onShow() {
		this.getCustomer()
	},
	onReachBottom() {
		this.page++
		let tagId = []
		this.tagsArr.map(item=>{
			tagId.push(item.id)
		})
		
		let qyUserId = []
		this.staffList.map(item=>{
			qyUserId.push(item.qy_wechat_id)
		})
		if(this.qyUserId){
			qyUserId.unshift(this.qyUserId)
		}
		
		if(this.moduleType == 'superHK'){ //超级管家
			let params = {
				staff_id:qyUserId,
				tag_ids:tagId,
				keyword:this.searchInput,
				page:this.page,
				pageSize:this.pagesize
			}
			GetTeamCustomer(params).then(res=>{
				if(res.code==1){
					if(!res.data.data.length){
						this.status = 'nomore'
					}else{
						this.status = 'loading'
					}
					this.customerList = this.customerList.concat(res.data.data)
				}
			})
		}else{
			let params = {
				qy_user_id:qyUserId,
				tag_ids:tagId,
				keyword:this.searchInput,
				page:this.page,
				pagesize:this.pagesize
			}
			console.log(params,'获取客户列表的数据')
			service_client.getCustomer(params).then(res=>{
				console.log(res,'客户列表')
				if(res.code==1){
					if(!res.data.data.length){
						this.status = 'nomore'
					}else{
						this.status = 'loading'
					}
					this.customerList = this.customerList.concat(res.data.data)
				}
			})
		}
	},
	methods: {
		// 获取客户列表、
		getCustomer(){
			let tagId = []
			this.tagsArr.map(item=>{
				tagId.push(item.qy_tag_id)
			})
			
			let qyUserId = []
			this.staffList.map(item=>{
				qyUserId.push(item.qy_wechat_id)
			})
			if(this.qyUserId){
				qyUserId.unshift(this.qyUserId)
			}
			
			if(this.moduleType == 'superHK'){ //超级管家
				let params = {
					staff_id:qyUserId,
					tag_ids:tagId,
					keyword:this.searchInput,
					page:this.page,
					pageSize:this.pagesize
				}
				GetTeamCustomer(params).then(res=>{
					if(res.code == 1){
						if(res.data.data.length<10){
							this.status = 'nomore'
						}
						this.customerList = res.data.data?res.data.data:[]
					}else{
						this.status = 'nomore'
					}
				})
				
			}else{
				let params = {
					qy_user_id:qyUserId,
					tag_ids:tagId,
					keyword:this.searchInput,
					page:this.page,
					pagesize:this.pagesize
				}
				console.log(params,'获取客户列表的数据')
				service_client.getCustomer(params).then(res=>{
					console.log(res,'客户列表')
					if(res.code==1){
						if(res.data.data.length<10){
							this.status = 'nomore'
						}
						this.customerList = res.data.data?res.data.data:[]
					}else{
						this.status = 'nomore'
					}
				})
			}
			
		},
		
		// 搜索客户列表
		searchFn(){
			this.getCustomer()
		},
		
		// 跳转到标签筛选
		toScreenTag(){
			uni.navigateTo({
				url:'/pages_client/pages/screenTag/index'
			})
		},
		// 跳转到添加人筛选
		toScreenStaff(){
			uni.navigateTo({
				url:'/pages_client/pages/screenStaff/index?moduleType='+this.moduleType
			})
		},
		
		// 清楚已选筛选
		closeSelect(type){
			if(type==1){
				this.staffList = []
			}else{
				this.tagsArr = []
			}
			this.getCustomer()
		},
		
		// 进入客户详情
		toDetail(staffUserId,customUserId){
			uni.navigateTo({
				url:`/pages_client/pages/coustomDetail/index?staffUserId=${staffUserId}&customUserId=${customUserId}`
			})
		},
		
		// 清除搜索内容
		clearInput(){
			if(this.searchInput==''){
				this.getCustomer()
			}
		}
	}
};
</script>
<style lang="scss">
	.staffCustomer{
		position: relative;
		.topBg{
			width: 100%;
			height: 308rpx;
			background: url(https://manager.ryz1620.com/static/adminShop/client/bg.png) no-repeat;
			background-size: 100%;
			position: absolute;
			top: 0;
			left: 0;
			.search{
				margin-top: 197rpx;
				padding:  0 45rpx 0 32rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				image{
					width: 40rpx;
					height: 40rpx;
				}
				.searchBox{
					padding:  0 29rpx;
					display: flex;
					align-items: center;
					width: 690rpx;
					height: 80rpx;
					background-color: #f1f1f1;
					border-radius: 40rpx;
					.iconfont{
						color: #878787;
					}
					input{
						width: 100%;
						margin-left: 19rpx;
						font: 500 24rpx/80rpx PingFang SC;
						color: #878787;
					}
				}
			}
		}
		.content{
			padding: 40rpx 0;
			.selectBox{
				display: flex;
				padding-left: 32rpx;
				.item{
					display: flex;
					align-items: center;
					padding: 20rpx 30rpx;
					background-color: #fff;
					margin-right: 20rpx;
					border-radius: 8rpx;
					.iconfont{
						font-size: 20rpx;
						margin-left: 10rpx;
					}
					.closeIcon{
						font-size: 36rpx;
					}
				}
			}
			.indexList{
				padding: 40rpx 32rpx;
				.item{
					display: flex;
					align-items: center;
					image{
						width: 95rpx;
						height: 95rpx;
						border-radius: 40%;
						flex-shrink: 0;
						margin-right: 32rpx;
					}
					.info{
						width: 100%;
						display: flex;
						justify-content: space-between;
						align-items: center;
						padding: 30rpx 0;
						border-bottom: 2rpx solid #F1F1F1;
						.name{
							font: bold 32rpx/32rpx PingFang SC;
							color: #333;
							.tagBox{
								display: flex;
								align-items: center;
								margin-top: 15rpx;
								text{
									padding: 5rpx 10rpx;
									border-radius: 2rpx;
									background-color: #F4EBDC;
									font: 500 22rpx/22rpx PingFang SC;
									color: #AE8C6C;
									margin-right: 10rpx;
								}
							}
						}
						.source{
							display: flex;
							flex-direction: column;
							font: 500 22rpx/40rpx PingFang SC;
							color: #999;
							text{
								text-align: right;
							}
						}
					}
					.active{
						padding: 52rpx 0;
					}
				}
				.u-load-more-wrap{
					padding: 20rpx 0;
				}
			}
		}
	}
</style>
